package com.sleepcamel.resteasyConfigurator.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

import com.sleepcamel.resteasyConfigurator.view.detail.DetailView;
import com.sleepcamel.resteasyConfigurator.view.sourcecode.ClassSourceCodeStrategy;
import com.sleepcamel.resteasyConfigurator.view.sourcecode.InterfaceSourceCodeStrategy;
import com.sleepcamel.resteasyConfigurator.view.sourcecode.SourceCodeView;

public class DetailSourceCodeComposite<T> extends DetailView<T> {

	private CTabFolder tabFolder;

	public DetailSourceCodeComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout());
	}

	public void setViews(DetailView<?> detailView, final SourceCodeView sourceCodeView){
		tabFolder = new CTabFolder(this, SWT.BORDER | SWT.BOTTOM);
		
		CTabItem detailItem = new CTabItem(tabFolder, SWT.NONE);
		detailItem.setText("Detail");
		detailView.setParent(tabFolder);
		detailItem.setControl(detailView);
		
		sourceCodeView.setParent(tabFolder);

		final CTabItem interfaceSourceItem = new CTabItem(tabFolder, SWT.NONE);
		interfaceSourceItem.setText("Interface Source");
		interfaceSourceItem.setControl(sourceCodeView);
		
		final CTabItem classSourceItem = new CTabItem(tabFolder, SWT.NONE);
		classSourceItem.setText("Class Source");
		classSourceItem.setControl(sourceCodeView);
		
		tabFolder.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				CTabItem selection = tabFolder.getSelection();
				boolean interfaceClicked = (selection == interfaceSourceItem);
				boolean classClicked = (selection == classSourceItem);
				if ( interfaceClicked || classClicked ){
					if ( interfaceClicked )
						sourceCodeView.setSourceCodeGenerateStrategy(new InterfaceSourceCodeStrategy());
					if ( classClicked )
						sourceCodeView.setSourceCodeGenerateStrategy(new ClassSourceCodeStrategy());
					sourceCodeView.refresh();
				}
			}
		});
		
		tabFolder.setSelection(detailItem);
	}
}
